package com.vedana.bankmobile;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.vedana.bankmobile.shared.Customer;

/**
 * Adapter for displaying a list of customers in a listview.
 * 
 * @author Didier MOISE
 *
 */
public class CustomersAdapter extends ArrayAdapter<Customer> {
	private Context context;
	private List<Customer> customers;
	
	/**
	 * Constructor.
	 * 
	 * @param context Activity
	 * @param customers List of customers
	 */
	public CustomersAdapter(Context context, List<Customer>customers) {
		super(context, R.layout.customer_row, customers);
		this.context = context;
		this.customers = customers;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View rowView = inflater.inflate(R.layout.customer_row, null, true);
		
		// Get UI fields
		TextView labelView = (TextView) rowView.findViewById(R.id.customerLabel);
		GridView gridView = (GridView) rowView.findViewById(R.id.accountsGrid);
		
		// Display customer
		Customer customer = customers.get(position);
	
		labelView.setText(customer.getLabel());
		
		// Fill the grid with accounts
		gridView.setAdapter(new AccountsAdapter(context, customer.getAccounts()));

		return rowView;
	}
}
